Insight from Common Indicators

library('quantmod')
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: TTR
## Version 0.4-0 included new data defaults. See ?getSymbols.
getSymbols(c('SPY', 'AMZN', 'QQQ'), src='yahoo')
## 'getSymbols' currently uses auto.assign=TRUE by default, but will
## use auto.assign=FALSE in 0.5-0. You will still be able to use
## 'loadSymbols' to automatically load data. getOption("getSymbols.env")
## and getOption("getSymbols.auto.assign") will still be checked for
## alternate defaults.
## 
## This message is shown once per session and may be disabled by setting 
## options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
## [1] "SPY"  "AMZN" "QQQ"
# remove any NAs 
SPY <- SPY[!(rowSums(is.na(SPY))),]
AMZN <- AMZN[!(rowSums(is.na(AMZN))),]
QQQ <- QQQ[!(rowSums(is.na(QQQ))),]

# install.packages('binhf')
library('binhf')
## Loading required package: wavethresh
## Loading required package: MASS
## WaveThresh: R wavelet software, release 4.6.8, installed
## Copyright Guy Nason and others 1993-2016
## Note: nlevels has been renamed to nlevelsWT
## Loading required package: adlift
## Loading required package: EbayesThresh
## 
##  **********************************************
##  adlift: a package to perform wavelet lifting schemes
## 
##  --- Written by Matt Nunes and Marina Knight ---
##    Current package version:  1.4-1  ( 2018-07-09 ) 
## 
##              -+ packaged by MAN +-           
##  **********************************************
##  
##  adlift 1.4-1 loaded
## 
## Attaching package: 'adlift'
## The following object is masked from 'package:EbayesThresh':
## 
##     postmean.cauchy
## 
##  **********************************************
##  binhf: Haar-Fisz functions for binomial data
## 
##  --- Written by Matt Nunes ---
##    Current package version:  1.0-3  ( 2018-07-18 ) 
## 
##  
##  **********************************************
##  
##  binhf 1.0-3 loaded
## 
## Attaching package: 'binhf'
## The following object is masked from 'package:EbayesThresh':
## 
##     negloglik.laplace
## The following object is masked from 'package:base':
## 
##     norm
library('TTR')

ADX

The ADX is Welles Wilder’s Directional Movement Indicator. It is used by lots of people to determine if the market is trending or range bound.

Blue line interpretation; Blue above 20 is a trend Red is the bearish indicator Green is bullish indicator You read the Red to Green as what’s going on, so to speak and the Blue for strength of trend

Example; Red over Green and Blue above 20 means strong bearish trend.

chartSeries(QQQ, theme="white", TA="addSMA(50, col='black');addSMA(200, col='blue');addADX(n = 14, maType='EMA', wilder=TRUE)", subset='2017::')

chartSeries(SPY, theme="white", TA="addSMA(50, col='black');addSMA(200, col='blue');addADX(n = 14, maType='EMA', wilder=TRUE)", subset='2017::')

In a nutshell, Welles recommends using the ADX with a 14-day period. When the main blue line is above 20, it is considered a strong, trending market, when it is below, it is considered a weak one.

Volume VWAP - directional indicator

As this evaluation is mostly using the closing price but it is important to note that there are a lot of other market variables available. You can design systems with the open price, the high or low, the difference between the open and close, etc. And there is also the volume.

This an important indicator. A falling stock price on rising volume or a rising stock on falling volume may mean the move is about to reverse. Whatever the reason for abnormal volume, it should be a warning to keep a vigilant eye on the stock.

There are plenty of indicators that include the volume price such as the Volume-weighted average price (VWAP). The VWAP is a guide more than a trading indicator as to where the market is trading compared to the volume adjusted price. It divides dollars traded by volume (see above link for more details). Pay attention to the zero line.

SPY.VWAP.Slow <- VWAP(price=SPY$SPY.Close, volume=SPY$SPY.Volume, n=100)
SPY.VWAP.Fast <- VWAP(price=SPY$SPY.Close, volume=SPY$SPY.Volume, n=20)
SPY.VWAP.Diff <- SPY.VWAP.Fast - SPY.VWAP.Slow
chartSeries(SPY, theme="white", subset='2017::',
            TA="addVo();addTA(SPY.VWAP.Slow, on=1, col='red');
              addTA(SPY.VWAP.Fast, on=1, col='blue');
              addTA(SPY.VWAP.Diff, col='blue');
              addADX(n = 14, maType='EMA', wilder=TRUE)")

# kick out pdf of last plot
dev.copy(pdf, "/Users/markloessi/InvestingCode/R_Investing/SPY-SPY-CLOSE-VWAP_ADX.pdf", width=10, height=7)
## pdf 
##   3
dev.off()
## quartz_off_screen 
##                 2
QQQ.VWAP.Slow <- VWAP(price=QQQ$QQQ.Close, volume=QQQ$QQQ.Volume, n=100)
QQQ.VWAP.Fast <- VWAP(price=QQQ$QQQ.Close, volume=QQQ$QQQ.Volume, n=20)
QQQ.VWAP.Diff <- QQQ.VWAP.Fast - QQQ.VWAP.Slow
chartSeries(QQQ, theme="white", subset='2017::',
            TA="addVo();addTA(QQQ.VWAP.Slow, on=1, col='red');
              addTA(QQQ.VWAP.Fast, on=1, col='blue');
              addTA(QQQ.VWAP.Diff, col='blue');
              addADX(n = 14, maType='EMA', wilder=TRUE)")

# kick out pdf of last plot
dev.copy(pdf, "/Users/markloessi/InvestingCode/R_Investing/QQQ-QQQ-CLOSE-VWAP_ADX.pdf", width=10, height=7)
## pdf 
##   3
dev.off()
## quartz_off_screen 
##                 2
AMZN.VWAP.Slow <- VWAP(price=AMZN$AMZN.Close, volume=AMZN$AMZN.Volume, n=100)
AMZN.VWAP.Fast <- VWAP(price=AMZN$AMZN.Close, volume=AMZN$AMZN.Volume, n=20)
AMZN.VWAP.Diff <- AMZN.VWAP.Fast - AMZN.VWAP.Slow
chartSeries(AMZN, theme="white", subset='2017::',
            TA="addVo();addTA(AMZN.VWAP.Slow, on=1, col='red');
              addTA(AMZN.VWAP.Fast, on=1, col='blue');
              addTA(AMZN.VWAP.Diff, col='blue');
              addADX(n = 14, maType='EMA', wilder=TRUE)")

# kick out pdf of last plot
dev.copy(pdf, "/Users/markloessi/InvestingCode/R_Investing/AMZN-AMZN-CLOSE-VWAP_ADX.pdf", width=10, height=7)
## pdf 
##   3
dev.off()
## quartz_off_screen 
##                 2

Make a trading system

AMZN.VWAP.Slow <- VWAP(price=AMZN$AMZN.Close, volume=AMZN$AMZN.Volume, n=100)
AMZN.VWAP.Fast <- VWAP(price=AMZN$AMZN.Close, volume=AMZN$AMZN.Volume, n=20)
AMZN.VWAP.Diff <- AMZN.VWAP.Fast - AMZN.VWAP.Slow

AMZN.ADX.14 <- ADX(AMZN,n=14)

Adjust your VWAP and ADX numbers to correspond to something meaningful for your stock.

# look for long entries
AMZN.Long_Trades <- ifelse(
        AMZN.ADX.14$ADX > 20 &
        AMZN.VWAP.Diff > 100, AMZN$AMZN.Close, NA)

# look for long entries
AMZN.Short_Trades <- ifelse(
        AMZN.ADX.14$ADX > 20 &
        AMZN.VWAP.Diff < -100, AMZN$AMZN.Close, NA)

plot(AMZN$AMZN.Close, subset='2017::')

## Warning in plot.xts(QQQ): only the univariate series will be plotted
points(AMZN.Long_Trades, col='blue', cex=1, pch=18)

points(AMZN.Short_Trades, col='red', cex=1, pch=18)

AMZN Kitchen Sink

AMZN.EMA.22<- EMA(AMZN$AMZN.Close, n=22)
AMZN.EMA.50<- EMA(AMZN$AMZN.Close, n=50)
AMZN.EMA.100<- EMA(AMZN$AMZN.Close, n=100)
AMZN.EMA.200<- EMA(AMZN$AMZN.Close, n=200)
AMZN.Fast.Diff <- AMZN.EMA.22 - AMZN.EMA.50
AMZN.Slow.Diff <- AMZN.EMA.50 - AMZN.EMA.200
chartSeries(AMZN, theme="white", subset='2018::',
            TA="addVo();
              addWPR(n=14);
              addMACD(type='EMA');
              addRSI(n = 14, maType='EMA', wilder=TRUE);
              addSMI(n = 13, slow = 25, fast = 2, signal = 9, ma.type='EMA'); #hmm... Stochastic Oscillator
              addADX(n = 14, maType='EMA', wilder=TRUE);
              addCCI();
              addBBands();
              addROC(n=200,col = 'blue');addROC(n=50,col = 'red');addROC(n=22,col = 'green') # rate of change
            ")

addTA(AMZN.EMA.22, on=1, col = "green")

addTA(AMZN.EMA.50, on=1, col = "yellow")

addTA(AMZN.EMA.100, on=1, col = "blue")

addTA(AMZN.EMA.200, on=1, col = "red")

addTA(AMZN.Slow.Diff, col='red', type='h',legend="Slow Diff 50-200 MA used for in-out of market")

addTA(AMZN.Fast.Diff, col='blue', type='h',legend="Fast Diff 22-50 MA used for in-out of market")

# kick out pdf of last plot
dev.copy(pdf, "/Users/markloessi/InvestingCode/R_Investing/AMZN-KitchenSink.pdf", width=10, height=20)
## pdf 
##   3
dev.off()
## quartz_off_screen 
##                 2